package com.frank.oj.controller.oj;

import com.frank.oj.annotation.AnonApi;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.model.dto.ApplyResetPasswordDTO;
import com.frank.oj.model.dto.LoginDTO;
import com.frank.oj.model.dto.RegisterDTO;
import com.frank.oj.model.dto.ResetPasswordDTO;
import com.frank.oj.model.vo.RegisterCodeVO;
import com.frank.oj.model.vo.UserInfoVO;
import com.frank.oj.service.oj.UserPassportService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户身份处理(登录、注册、退出、重置密码）
 *
 * @author frank
 * @Date 2024/1/25
 */
@RestController
@RequestMapping("/api")
public class UserPassportController {

    @Resource
    private UserPassportService userPassportService;

    /**
     * 登录
     *
     * @param loginDTO 登录信息
     * @param response 响应对象
     * @param request  请求对象
     * @return CommonResult
     */
    @PostMapping("/login")
    @AnonApi
    public CommonResult<UserInfoVO> login(@Validated @RequestBody LoginDTO loginDTO, HttpServletResponse response, HttpServletRequest request) {
        return userPassportService.login(loginDTO, request, response);
    }

    /**
     * 注册
     *
     * @param registerDTO 注册信息
     */
    @PostMapping("/register")
    @AnonApi
    public CommonResult<Void> register(@Validated @RequestBody RegisterDTO registerDTO) {
        return userPassportService.register(registerDTO);
    }

    @GetMapping("/get-register-code")
    @AnonApi
    public CommonResult<RegisterCodeVO> getRegisterCode(@RequestParam(value = "email", required = true) String email) {
        return userPassportService.getRegisterCode(email);
    }

    /**
     * 退出，清除redis中的jwt
     *
     * @return CommonResult
     */
    @GetMapping("/logout")
    @RequiresAuthentication
    public CommonResult<Void> logout() {
        return userPassportService.logout();
    }

    /**
     * 重置密码邮件
     */
    @PostMapping("/apply-reset-password")
    @AnonApi
    public CommonResult<Void> applyResetPassword(@RequestBody ApplyResetPasswordDTO applyResetPasswordDto) {
        return userPassportService.applyResetPassword(applyResetPasswordDto);
    }

    /**
     * 重置密码
     */
    @PostMapping("/reset-password")
    @AnonApi
    public CommonResult<Void> resetPassword(@RequestBody ResetPasswordDTO resetPasswordDto) {
        return userPassportService.resetPassword(resetPasswordDto);
    }

}
